<template>
	<view id="box">
	    <view id="content">
	        <view>
	            <image src="/static/logo.png" mode="widthFix" style="width:500rpx;margin-bottom:50rpx;"></image>
	        </view>
	        <view style="margin-bottom:50rpx;">
	            <text style="color:#999999;">需要获取您的手机号才能服务</text>
	        </view>
	        <view>
	            <button type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">允许</button>
	        </view>
	    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: {},
				providerList: [],
				phoneInfo: {},
				user: {},
			}
		},
		onLoad(){
			var _this = this;
			// uni.getProvider({
			//     service: 'oauth',
			//     success: function (res) {
			//         //console.log(res);
			// 		var newList = [];
			// 		res.provider.forEach(e => {
			// 			var newItem = _this.providerType(e);
			// 			newList.push({
			// 				name: newItem.name,
			// 				id: newItem.id
			// 			});
			// 		})
			// 		_this.providerList = newList;
			//     },
			// 	fail: (error) => {
			// 		console.log('获取登录通道失败', error);
			// 	}
			// });
			wx.login({
				success: res =>{
					_this.data.code = res.code
				}
			});
			//console.log(_this.providerList);
		},
		methods: {
			Toast(data, duration = 1000) {
				uni.showToast(Object.assign({}, data, {
					duration
				}))
			},
			providerType(value){
				let providerName = '';
				switch (value) {
					case 'weixin':
						providerName = '微信登录'
						break;
					case 'qq':
						providerName = 'QQ登录'
						break;
					case 'sinaweibo':
						providerName = '新浪微博登录'
						break;
					case 'xiaomi':
						providerName = '小米登录'
						break;
					case 'alipay':
						providerName = '支付宝登录'
						break;
					case 'baidu':
						providerName = '百度登录'
						break;
					case 'toutiao':
						providerName = '头条登录'
						break;
					case 'apple':
						providerName = '苹果登录'
						break;
					case 'univerify':
						providerName = '一键登录'
						break;
				}
				return {
					name: providerName,
					id: value
				}
			},
			getPhoneNumber(event) {
				var that = this;
				var detail = event.detail;
				if(detail.errMsg == "getPhoneNumber:ok"){
					this.phoneInfo = {
						iv: detail.iv,
						encryptedData: detail.encryptedData
					};
					// console.log(this.phoneInfo);
					var data = {
						code: this.data.code,
						userInfo: uni.getStorageSync("wxUserInfo"),
						// phoneInfo: JSON.stringify(this.phoneInfo)
						encryptedData: detail.encryptedData,
						iv: detail.iv
					}
					// #ifdef MP-WEIXIN
					var signData = that.$request.run("login","");
					uni.request({
						url: signData.url,
						method: signData.method,
						data: data,
						header: signData.header,
						success(response) {
							// console.log(response);
							if (response.data.code != 0) {
								console.log('获取openid失败：', response.data.errMsg);
								return
							}
							var token = response.data.token;
							uni.setStorageSync("token",token);
							uni.showToast({
								title: "登录成功",
								duration: 300,
							});
							uni.navigateBack({
								detail: 1
							});
							//uni.setStorageSync('openid', res.data.openid)
						},
						fail(err) {
							console.log('获取openid失败：', err);
						}
					})
					// #endif
				}
				else{
					uni.showModal({
						title: "信息",
						content: "您拒绝了微信授权",
						showCancel: false,
						confirmText: "确定"
					});
				}
			},
			// login(provider) {
			// 	var _this = this;
			// 	uni.login({
			// 		provider: provider.id,
			// 		success: function (loginRes) {
			// 			console.log(loginRes);
			// 			var data = {
			// 				code: loginRes.code,
			// 				userInfo: JSON.stringify(uni.getStorageSync("wxUserInfo")),
			// 				phoneInfo: JSON.stringify(_this.phoneInfo)
			// 			}
			// 			// #ifdef MP-WEIXIN
			// 			console.warn('如需获取openid请参考uni-id: https://uniapp.dcloud.net.cn/uniCloud/uni-id')
			// 			uni.request({
			// 				url: _this.$config.domain + 'Api/WeiXinSpUser/login',
			// 				method: 'POST',
			// 				data: _this.$sign.sign(_this.$config.appkey,data),
			// 				header: {"content-type": "application/x-www-form-urlencoded"},
			// 				success(response) {
			// 					console.log(response);
			// 					if (response.data.code !== 0) {
			// 						console.log('获取openid失败：', response.data.msg);
			// 						return
			// 					}
			// 					var token = response.data.data.token;
			// 					uni.setStorageSync("token",token);
			// 					uni.showModal({
			// 						title: "信息",
			// 						content: "登录成功",
			// 						showCancel: false,
			// 						confirmText: "确定",
			// 						success: function (res) {
			// 							if (res.confirm) {
			// 								uni.switchTab({
			// 									url: 'user',
			// 								})
			// 							} else if (res.cancel) {
			// 								uni.navigateTo({
			// 									url: '/pages/user/get-phone'
			// 								})
			// 							}
			// 						}
			// 					});
			// 					//uni.setStorageSync('openid', res.data.openid)
			// 				},
			// 				fail(err) {
			// 					console.log('获取openid失败：', err);
			// 				}
			// 			})
			// 			// #endif
			// 		}
			// 	});
			// }
		}
	}
	
</script>

<style>
	#box {
	    position: absolute;
	    left: 0;
	    top: 0;
	    width: 100%;
	    height: 100%;
	    display: flex;
	}
	#content {
	    margin: auto;
	    text-align: center;
	}
</style>
